package com.lancoo.znbkxx.base;

import android.content.Context;
import android.util.DisplayMetrics;
import android.view.WindowManager;

/**
 * Create by TangJun on 2019/2/16
 */
public class AppContext {
    private static AppContext appContext;
    private static int WindowWidth = 0;
    private static int WindowHeight = 0;
    private AppSettingsBean settingsBean;
    private PersistentDataCenter persistentDataCenter;
    private AppContext(Context context) {
        persistentDataCenter = new PersistentDataCenter();
        persistentDataCenter.init(context);
    }

    public AppSettingsBean getSettingsBean() {
        if (settingsBean == null) {
            settingsBean = getPersistentDataCenter().getAppSettingBeanFromSource();
        }
        return settingsBean;
    }

    public static AppContext getInstance(Context context) {
        if (appContext == null) {
            appContext = new AppContext(context);
        }
        return appContext;
    }

    public String getBaseUrl() {
        return "http://" + getSettingsBean().getBaseIp() + ":" + getSettingsBean().getBasePort() + "/";
    }

    public PersistentDataCenter getPersistentDataCenter() {
        return persistentDataCenter;
    }


    public void ReFresh() {
        settingsBean = null;
    }


    public int getWindowWidth(Context context) {
        if (WindowHeight * WindowWidth == 0) {
            WindowManager m = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            DisplayMetrics metrics = new DisplayMetrics();
            m.getDefaultDisplay().getMetrics(metrics);
            WindowWidth = metrics.widthPixels;
            WindowHeight = metrics.heightPixels;
        }
        return WindowWidth;
    }

    public int getWindowHeight(Context context) {
        if (WindowHeight * WindowWidth == 0) {
            WindowManager m = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            DisplayMetrics metrics = new DisplayMetrics();
            m.getDefaultDisplay().getMetrics(metrics);
            WindowWidth = metrics.widthPixels;
            WindowHeight = metrics.heightPixels;
        }
        return WindowHeight;
    }
}

